import 'dart:convert' show json;

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }

  return null;
}

class GankCategoryListData {
  GankCategoryListData({
    this.data,
    this.page,
    this.pageCount,
    this.status,
    this.totalCounts,
  });

  factory GankCategoryListData.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<Category> data = jsonRes['data'] is List ? <Category>[] : null;
    if (data != null) {
      for (final dynamic item in jsonRes['data']) {
        if (item != null) {
          data.add(Category.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }
    return GankCategoryListData(
      data: data,
      page: asT<int>(jsonRes['page']),
      pageCount: asT<int>(jsonRes['page_count']),
      status: asT<int>(jsonRes['status']),
      totalCounts: asT<int>(jsonRes['total_counts']),
    );
  }

  List<Category> data;
  int page;
  int pageCount;
  int status;
  int totalCounts;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'data': data,
        'page': page,
        'page_count': pageCount,
        'status': status,
        'total_counts': totalCounts,
      };
  @override
  String toString() {
    return json.encode(this);
  }
}

class Category {
  Category({
    this.id,
    this.author,
    this.category,
    this.createdAt,
    this.desc,
    this.images,
    this.likeCounts,
    this.publishedAt,
    this.stars,
    this.title,
    this.type,
    this.url,
    this.views,
  });

  factory Category.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<String> images = jsonRes['images'] is List ? <String>[] : null;
    if (images != null) {
      for (final dynamic item in jsonRes['images']) {
        if (item != null) {
          images.add(asT<String>(item));
        }
      }
    }
    return Category(
      id: asT<String>(jsonRes['_id']),
      author: asT<String>(jsonRes['author']),
      category: asT<String>(jsonRes['category']),
      createdAt: asT<String>(jsonRes['createdAt']),
      desc: asT<String>(jsonRes['desc']),
      images: images,
      likeCounts: asT<int>(jsonRes['likeCounts']),
      publishedAt: asT<String>(jsonRes['publishedAt']),
      stars: asT<int>(jsonRes['stars']),
      title: asT<String>(jsonRes['title']),
      type: asT<String>(jsonRes['type']),
      url: asT<String>(jsonRes['url']),
      views: asT<int>(jsonRes['views']),
    );
  }

  String id;
  String author;
  String category;
  String createdAt;
  String desc;
  List<String> images;
  int likeCounts;
  String publishedAt;
  int stars;
  String title;
  String type;
  String url;
  int views;

  Map<String, dynamic> toJson() => <String, dynamic>{
        '_id': id,
        'author': author,
        'category': category,
        'createdAt': createdAt,
        'desc': desc,
        'images': images,
        'likeCounts': likeCounts,
        'publishedAt': publishedAt,
        'stars': stars,
        'title': title,
        'type': type,
        'url': url,
        'views': views,
      };
  @override
  String toString() {
    return json.encode(this);
  }
}
